package me.zhengjie.modules.account.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import me.zhengjie.modules.account.domain.AccountAa03;
import me.zhengjie.modules.account.domain.AccountAc50;
import me.zhengjie.modules.account.domain.AccountAc51;
import me.zhengjie.modules.account.factory.service.AccountComputer;
import me.zhengjie.modules.account.factory.service.AccountComputerTransfer;
import me.zhengjie.modules.account.factory.service.AccountPrintService;
import me.zhengjie.modules.account.mapper.*;
import me.zhengjie.modules.account.service.AccountAc50Service;
import me.zhengjie.modules.account.service.AccountService;
import me.zhengjie.modules.basic.domain.BasicAc02;
import me.zhengjie.modules.basic.mapper.BasicAb01Mapper;
import me.zhengjie.modules.basic.mapper.BasicAc01Mapper;
import me.zhengjie.modules.basic.service.BasicAc02Service;
import me.zhengjie.modules.logs.domain.BusinessLog;
import me.zhengjie.modules.logs.service.BusinessLogService;
import me.zhengjie.modules.sequence.domain.Sequence;
import me.zhengjie.modules.sequence.service.SequenceService;
import me.zhengjie.tool.*;
import me.zhengjie.utils.SecurityUtils;
import me.zhengjie.vo.Constant;
import me.zhengjie.vo.YHConstants;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 个人账户实现类
 */
@Service
@RequiredArgsConstructor
public class AccountServiceImpl  implements AccountService {

    private final AccountComputer accountComputer;
    private final AccountComputerTransfer accountComputerTransfer;
    private final AccountCommonMapper accountCommonMapper;
    private final AccountPrintService accountPrintService;
    private final AccountAc50Service accountAc50Service;
    private final BasicAc02Service basicAc02Service;
    private final BasicAc01Mapper basicAc01Mapper;
    private final SequenceService sequenceService;
    private final BusinessLogService businessLogService;


    @Override
    @Transactional(rollbackFor = Exception.class)
    public MyMap saveInterest(MyMap businessMap) throws IOException {

        //个人账户计息
        accountComputerTransfer.computerAccount(businessMap);


        MyMap query = new MyMap<>();
        query.put("aae140",businessMap.get("aae140"));
        query.put("aac001",businessMap.get("aac001"));
        query.put("aaz159",businessMap.get("aaz159"));
        //个人账户打印
     //   accountPrintService.printAccount(query);

        //写日志
        Map map= new HashMap<>();
        String yyyyMMddHHmmss = DateUtil.getCurrentTime("yyyyMMddHHmmss");

        //查询人员基本信息

        Map q = new HashMap();
        q.put("aac001",businessMap.get("aac001"));
        List<Map> list1 = accountCommonMapper.queryAc01Ac02(q);




        map.put("aaz002",sequenceService.nextSequence(new Sequence("aaz002")));
        map.put("aaa121", YHConstants.AAA121_020107);
        map.put("aab001","");
        map.put("aab004","");
        map.put("aac001",businessMap.get("aac001"));
        map.put("aac002",list1.get(0).get("aac002"));
        map.put("aac003",list1.get(0).get("aac003"));
        map.put("aae140",Constant.AAE140_120_JGSYDWGZRYJBYLBX);
        map.put("aae016",Constant.AAE016_1_SHTG);
        map.put("aae011", SecurityUtils.getCurrentUsername());
        map.put("aae036",yyyyMMddHHmmss);
        map.put("aae014",SecurityUtils.getCurrentUsername());
        map.put("aae015",yyyyMMddHHmmss);
        map.put("aab359","530111");

        //7、记录日志信息
        BusinessLog businessLog = BusinessLog.singleLog(map);
        businessLogService.save(businessLog);


        return null;

    }

    /**
     * 打印个人账户
     * @param businessMap
     * @return
     */
    @Override
    public MyMap print(MyMap businessMap) {
        NPETool.isEmptyThrowException(businessMap.get("aaz159"),"参保关系ID");
        NPETool.isEmptyThrowException(businessMap.get("aac062"),"账户类型");

        List<Map> maps = accountCommonMapper.queryAc01Ac02(businessMap);
        businessMap.put("aaz159",businessMap.get("aaz159"));
        businessMap.put("aae140",maps.get(0).get("aae140"));
        businessMap.put("aac001",maps.get(0).get("aac001"));
        businessMap.put("aac062",businessMap.get("aac062"));
        //个人账户打印
      return   accountPrintService.printAccount(businessMap);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public MyMap saveInterest2(MyMap businessMap) {


        return null;
    }

    /**
     * 终止账户
     * @param businessMap
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean terminationAccount(MyMap businessMap) {

        NPETool.isEmptyThrowException(businessMap.getAsString("aaz159"),"人员参保关系ID");

        UpdateWrapper<AccountAc50> upad50 = new UpdateWrapper<>();
        if(Constant.AIC113_10_ZYNJZY.equals(businessMap.getAsString("aic113"))){
            upad50.eq("aac062", Constant.AAC062_9_QT);
        }else{
            upad50.eq("aac062", Constant.AAC062_1_RYXCB);
        }

        upad50.eq("aaz159", businessMap.getAsString("aaz159"));
        upad50.set("aae100", Constant.AAE100_0_WX);

        return accountAc50Service.update(upad50);

    }

    /**
     * 终止参保
     * @param businessMap
     * @return
     */
    @Override
    public boolean terminationBasic(MyMap businessMap) {
        NPETool.isEmptyThrowException(businessMap.getAsString("aaz159"),"人员参保关系ID");

        QueryWrapper<AccountAc50> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("aaz159",businessMap.getAsString("aaz159"));
        queryWrapper.eq("aae100",Constant.AAE100_1_YX);
        List<AccountAc50> list = accountAc50Service.list(queryWrapper);
        if(list.size()<=0){//只有所有的账户都无效了 才终止参保

        UpdateWrapper<BasicAc02> upac02 = new UpdateWrapper<>();
        upac02.eq("aaz159", businessMap.getAsString("aaz159"));
        upac02.set("aac008",Constant.AAC008_4_ZZCB);
        upac02.set("aac031",Constant.AAC031_3_ZZJF);

            return basicAc02Service.update(upac02);
        }else{
            return true;
        }


    }

    /**
     * 终止参保撤销
     * @param businessMap
     * @return
     */
    @Override
    public boolean terminationBasicRevoke(MyMap businessMap) {
        UpdateWrapper<BasicAc02> upac02 = new UpdateWrapper<>();
        upac02.eq("aaz159", businessMap.getAsString("aaz159"));
        upac02.set("aac008",Constant.AAC008_1_ZCCB);
        upac02.set("aac031",Constant.AAC031_2_ZTJF);

        return basicAc02Service.update(upac02);
    }

    /**
     * 终止账户撤销
     * @param businessMap
     * @return
     */
    @Override
    public boolean terminationAccountRevoke(MyMap businessMap) {
        NPETool.isEmptyThrowException(businessMap.getAsString("aaz159"),"人员参保关系ID");

        UpdateWrapper<AccountAc50> upad50 = new UpdateWrapper<>();
        upad50.eq("aaz159", businessMap.getAsString("aaz159"));

        if(Constant.AAE803_T1302_JGSYYLBXZYNJZYXXB.equals(businessMap.getAsString("aae803"))){
            upad50.eq("aac062", Constant.AAC062_9_QT);
        }else{
            upad50.eq("aac062", Constant.AAC062_1_RYXCB);
        }

        upad50.set("aae100", Constant.AAE100_1_YX);

        return accountAc50Service.update(upad50);
    }
}
